package team.hyznrj.studentsys.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.hyznrj.studentsys.dao.mapper.AccountInfoMapper;
import team.hyznrj.studentsys.dao.repository.account.PowerFunctionTableRepository;
import team.hyznrj.studentsys.dao.repository.account.PowerInfoRepository;
import team.hyznrj.studentsys.dao.repository.account.RolePowerTableRepository;
import team.hyznrj.studentsys.dto.PowerDto;
import team.hyznrj.studentsys.entity.account.FunctionInfo;
import team.hyznrj.studentsys.entity.account.PowerFunctionTable;
import team.hyznrj.studentsys.entity.account.PowerInfo;
import team.hyznrj.studentsys.entity.account.RolePowerTable;
import team.hyznrj.studentsys.entity.account.key.PowerFunctionTableKey;
import team.hyznrj.studentsys.enums.AccountEnum;
import team.hyznrj.studentsys.exception.AccountException;
import team.hyznrj.studentsys.service.PowerService;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author ReMidDream
 * @date 2018-05-07 19:14
 **/
@Service
@Slf4j
public class PowerServiceImpl implements PowerService {

    @Autowired
    private AccountInfoMapper accountInfoMapper;
    @Autowired
    private PowerFunctionTableRepository powerFunctionTableRepository;
    @Autowired
    private PowerInfoRepository powerInfoRepository;
    @Autowired
    private RolePowerTableRepository rolePowerTableRepository;

    @Override
    public PowerDto findPowerDtoByPowerId(String powerId) {
        return accountInfoMapper.findPowerDtoByPowerId(powerId);
    }

    @Override
    public List<PowerDto> findPowerDtoByPowerIds(Set<String> powerId) {
        return accountInfoMapper.findPowerDtoByPowerIds(powerId);
    }

    @Override
    public List<PowerDto> findAllPowerDto() {
        return accountInfoMapper.findAllPowerDto();
    }

    @Override
    public void insertRelationshipBetweenPowerAndFunction(PowerFunctionTableKey powerFunctionTableKey) {

        //todo 合法性认证

        PowerFunctionTable powerFunctionTable = new PowerFunctionTable();
        powerFunctionTable.setId(powerFunctionTableKey);

        powerFunctionTableRepository.save(powerFunctionTable);
    }

    @Override
    public void deleteRelationshipBetweenPowerAndFunction(PowerFunctionTableKey powerFunctionTableKey) {
        powerFunctionTableRepository.delete(powerFunctionTableKey);
    }

    @Override
    @Transactional
    public void deletePower(String powerId) {

        List<RolePowerTable> rolePowerTables = rolePowerTableRepository.findByIdRptPowerId(powerId);

        if (rolePowerTables.size() >0) {
            log.error(" @删除权限 : 此功能已被角色引用 PowerId={}",powerId);
            throw new AccountException(AccountEnum.TABLE_ERROR);
        }

        powerInfoRepository.delete(powerId);
        powerFunctionTableRepository.deleteByIdPftPowerId(powerId);
    }

    @Override
    @Transactional
    public void insertUpdatePower(PowerDto powerDto) {
        PowerInfo powerInfo = new PowerInfo();
        List<PowerFunctionTable> powerFunctionTables = new ArrayList<>();
        /*
            权限表 插入/更新
         */
        BeanUtils.copyProperties(powerDto,powerInfo);
        powerInfoRepository.save(powerInfo);
        /*
            关系表 插入/更新
         */
        if (powerDto.getFunctionInfoList() != null) {

            List<PowerFunctionTable> powerFunctionTableList = powerFunctionTableRepository.findByIdPftPowerId(powerDto.getPowerId());

            if (powerFunctionTableList.size() != 0) {
                powerFunctionTableRepository.deleteByIdPftPowerId(powerDto.getPowerId());
            }

            for (FunctionInfo functionInfo : powerDto.getFunctionInfoList()) {
                PowerFunctionTable powerFunctionTable = new PowerFunctionTable();
                PowerFunctionTableKey powerFunctionTableKey = new PowerFunctionTableKey();

                powerFunctionTableKey.setPftPowerId(powerDto.getPowerId());
                powerFunctionTableKey.setPftFunctionId(functionInfo.getFunctionId());

                powerFunctionTable.setId(powerFunctionTableKey);
                powerFunctionTables.add(powerFunctionTable);
            }
            powerFunctionTableRepository.save(powerFunctionTables);
        }
    }
}
